package com.oa.xinxi.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.oa.login.entity.Emp;
import com.oa.office.util.UtilHelp;
import com.oa.xinxi.biz.DepBiz;
import com.oa.xinxi.biz.PPEBiz;
import com.oa.xinxi.entity.Dep;
import com.oa.xinxi.entity.PPE;
import com.oa.xinxi.utils.Constants;

@Controller
@RequestMapping("/ppe")
public class PPEController {
	@Resource
	private PPEBiz ppeBiz;
	@Resource
	private DepBiz depBiz;
	@Resource
	private UtilHelp utilHelp;
//	@RequestMapping("/depList")
//	public String depList(Model model) {
//		List<Dep> list = depBiz.findAll();
//		model.addAttribute("list", list);
//		return "/xinxi/ppe/depList";
//	}
	@RequestMapping("/ppeList")
	public String ppeList(Model model) {
		List<PPE> list = ppeBiz.findAll();
		model.addAttribute("list", list);
		return "/xinxi/ppe/ppeList";
	}
	@RequestMapping("/preAdd")
	public String ppeAdd(Model model ) {
		List<Dep> list = depBiz.findAll();
		model.addAttribute("list", list);
		return "/xinxi/ppe/add";
	}
	@RequestMapping("/add")
	public String ppeAdd(PPE ppe) throws Exception, Exception {
		ppeBiz.save(ppe);
		return "redirect:/ppe/ppeList";
	}
	@RequestMapping("/preExcel")
	public String preUploadExcel() {
		return "/xinxi/ppe/excel";
	}
	/**
	 * @param request
	 * @param session
	 * @return
	 * @throws Exception
	 * @throws Exception
	 * 导入Excel
	 */
	@RequestMapping("/excel")
	public String insertExcel(MultipartHttpServletRequest request,HttpSession session) throws Exception, Exception {
		MultipartFile file = request.getFile("file");
		String fileName = UUID.randomUUID().toString();
		String name = file.getOriginalFilename();
		String path = utilHelp.getPath();
		//获取后缀名
		String extension = name.lastIndexOf(".") == -1 ? "" : name
				.substring(name.lastIndexOf(".") + 1);
		File newFile = null;
		if ("xls".equals(extension)) {
			newFile = new File(path + fileName+".xls");
		} else if ("xlsx".equals(extension)) {
			newFile = new File(path + fileName+".xlsx");
		}
		file.transferTo(newFile);
			if (null!=file) {
				Emp emp = (Emp)session.getAttribute("emp");
				try {
					ppeBiz.insertExcel(newFile,emp.getEmpName());
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		return "redirect:/ppe/ppeList";
	}
	/**
	 * @throws Exception
	 * @throws Exception
	 * 导出为Excel
	 */
	@ResponseBody
	@RequestMapping("/transtoexcel")
	public void transtoexcel() throws Exception, Exception {
		List<PPE> list = ppeBiz.findAll();
		ppeBiz.transtoexcel(list);
//		return "success";
	}
}
